<?php

class dbaSubscription
{

    var $dba;

    function dbaSubscription (&$dba)
    {
        $this->dba = &$dba;
    }
    
    function insert ($instance, $banner_id, $year, $week)
    {
        $this->dba->query("insert into ".$this->dba->tableforwarder.
            "_mod_banner_subscription (instance, banner_id, year, week) values (" .
            "'$instance',$banner_id, $year, $week);");
    }
    
    function delete ($instance, $id)
    {
        $this->dba->query("delete from ".$this->dba->tableforwarder.
            "_mod_banner_subscription where id = $id;");
    }
    
    function getCapacities ($instance, $year, $week, $limit)
    {
        $cond = $year * 100 + $week;
        $sth = $this->dba->query("select sum(BB.size) As used_capacity, week, year from ".
            $this->dba->tableforwarder."_mod_banner_subscription BS, ".
            $this->dba->tableforwarder."_mod_banner_banner BB " .
            "where BS.banner_id=BB.id and year*100+week >= $cond group by year, " .
            "week order by year, week limit $limit;");
        if (is_object($sth))
        {
            return $sth->fetch_rows_array();
        }
    }
    
    function getByBanner ($instance, $banner_id, $year, $week, $limit)
    {
        $cond = $year * 100 + $week;
        $sth = $this->dba->query("select distinct week, year from ".$this->dba->tableforwarder.
            "_mod_banner_subscription where year*100+week >= $cond and " .
            "banner_id = $banner_id and instance = '$instance' order by year, week limit $limit;");
        if (is_object($sth))
        {
            return $sth->fetch_rows_array();
        }
    }
    
    function getall($instance, $year, $week, $limit)
    {
        $cond = $year * 100 + $week;
        $sth = $this->dba->query("select BS.id As bs_id, instance, banner_id, year, week, " .
                "cust_id, size, alttext, type, C.name from ".
            $this->dba->tableforwarder."_mod_banner_subscription BS,".
            $this->dba->tableforwarder."_mod_banner_banner BB, " .
            $this->dba->tableforwarder."_mod_banner_customer C " .
            "where C.id = cust_id and BS.banner_id = BB.id and year*100+week >= $cond " .
            "order by year,week;");
        if (is_object($sth))
        {
            return $sth->fetch_rows_array();
        }
    }
    
    function getRandomOrderByInstance ($instance)
    {
        $week = date("W", time());
        $sth = $this->dba->query("select * from ".
            $this->dba->tableforwarder."_mod_banner_subscription BS, ".
            $this->dba->tableforwarder."_mod_banner_banner BB where " .
            "BS.banner_id = BB.id and week=$week and instance='$instance' order by rand();");    
        if (is_object($sth))
        {
            return $sth->fetch_rows_array();
        }
    }

}

?>